﻿using System;
using System.Collections.Generic;
using System.Text;

using SharpDX.DirectWrite;
using SharpDX.Direct2D1;
using SharpDX;

namespace SpaceBattle
{
    class Text
    {
        public TextFormat TextFormat { get; private set; }
        public TextLayout TextLayout { get; private set; }

        private RenderTarget renderTarget;
        private SharpDX.DirectWrite.Factory factory;
        private string Content = "";

        public void SetContent(String content)
        {
            Content = content;
            UpdateTextLayout();
        }

        // Recreates textlayout. Needs to be called each time content changes
        private void UpdateTextLayout()
        {
            TextLayout.Dispose();
            TextLayout = new TextLayout(factory, Content, TextFormat,
                renderTarget.PixelSize.Width, renderTarget.PixelSize.Height);
        }

        public Text(RenderTarget renderTarget, SharpDX.DirectWrite.Factory factory)
        {
            this.renderTarget = renderTarget;
            this.factory = factory;

            // Initialize a TextFormat
            TextFormat = new TextFormat(factory, "Arial", 14) 
              { TextAlignment = TextAlignment.Leading, ParagraphAlignment = ParagraphAlignment.Near };

            renderTarget.TextAntialiasMode = TextAntialiasMode.Cleartype;

            // Initialize a TextLayout
            TextLayout = new TextLayout(factory, Content, TextFormat,
                renderTarget.PixelSize.Width, renderTarget.PixelSize.Height);
        }

        public void Draw(DrawingPointF origin, Brush brush)
        {
            renderTarget.DrawTextLayout(origin, TextLayout, brush, DrawTextOptions.None);
        }

        public void AddContent(string additionalContent)
        {
            Content += additionalContent;
            UpdateTextLayout();
        }
    }
}
